################################# mac ####################################

# JAVA相关:
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_111.jdk/Contents/Home
export PATH=$JAVA_HOME/bin:$PATH
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar

# flutter相关:
export FLUTTER_STORAGE_BASE_URL=https://storage.flutter-io.cn
export PUB_HOSTED_URL=https://pub.flutter-io.cn
export PATH="$PATH:$HOME/dev/flutter/bin"

# android相关:
export PATH="$PATH:$HOME/Library/Android/sdk/platform-tools"
export PATH="$PATH:$HOME/Library/Android/sdk/emulator"
export PATH="$PATH:$HOME/Library/Android/sdk/tools"
export PATH="$PATH:$HOME/Library/Android/sdk/tools/bin"

# go相关:
export GOPATH="$HOME/dev/go"
export GOPROXY=https://goproxy.cn
export PATH=$GOPATH/bin:$PATH
export GOPRIVATE="gitee.com/elonelon/*"

# npm相关：
export PATH=$HOME/.npm-global/bin:$PATH

# yarn相关：
export PATH=$HOME/.yarn/bin:$PATH

# php相关：
export PATH=/opt/bin:$PATH

# 获取全局安装位置:
alias yarn_global_bin_path="yarn global bin"
alias yarn_global_dir_path="yarn global dir"

alias npm_global_bin_path="npm -g bin"
alias npm_global_dir_path="npm -g root"

# vue相关：
vue_create() {
    vue create
}

# 查看端口被哪个进程调用:
# psearch(){
#     lsof -i -P | grep :"$1"
# }

# 更改hosts文件后使生效:
host_refresh() {
    killall -HUP mDNSResponder
}

# 删除：
del_DerivedData() {
    rm -rf ~/Library/Developer/Xcode/DerivedData
}

# vs() {open $1 -a /Applications/Visual\ Studio\ Code.app}

finder() {open $1 -a finder}
fr() {open $1 -a finder}
vsenv() {open ~/.zshrc -a /Applications/Visual\ Studio\ Code.app}

alias awk="gawk"

## ssh:

ssh_cat() {
    cat ~/.ssh/id_rsa.pub
    pbcopy <~/.ssh/id_rsa.pub
    echo 公钥已经复制到剪切板！
}

debian_docker_set_mirror() {
    setting_file="/etc/docker/daemon.json"
    cat >$setting_file <<EOF
    {
        "registry-mirrors": [
            "https://registry.cn-hangzhou.aliyuncs.com",
            "https://mirror.ccs.tencentyun.com",
            "https://registry.docker-cn.com",
            "http://hub-mirror.c.163.com",
            "https://docker.mirrors.ustc.edu.cn"
        ]
    }
EOF
}

debian_install_docker_ce() {
    if which docker >/dev/null 2>&1; then
        echo "--- docker is installed ---"
        docker --version
        docker-compose --version
        return 0
    fi
    source="deb [arch=amd64] http://mirrors.aliyun.com/docker-ce/linux/debian buster stable"
    apt remove docker docker-engine docker.io containerd runc || true
    apt -y install sudo apt-transport-https ca-certificates curl gnupg-agent software-properties-common
    curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/debian/gpg | sudo apt-key add -
    sed -i "/$($source)/d" /etc/apt/sources.list
    echo $source >>/etc/apt/sources.list
    apt update && sudo apt install -y docker-ce docker-compose
    docker --version
    docker-compose --version
}

ssh_install_docker-ce() {
    if [ $# != 1 ]; then
        echo "#### 使用方法: ssh_install_docker-ce root@192.168.1.1"
    fi
    remote=$1
    ssh $remote <<EOF
        $(typeset -f)
        debian_install_docker_ce $remote
EOF
}

ssh_set_alias() {
    if [ $# != 1 ]; then
        echo "#### 使用方法: ssh_install_docker-ce root@192.168.1.1"
    fi
    remote=$1
    ssh $remote <<EOF
        $(typeset -f debian_set_alias)
        debian_set_alias
EOF
}

set.alias() {
    curl -s https://gitee.com/elonelon/dotfiles/raw/master/alias/mac.sh >"$HOME"/.mac.sh &&
        curl -s https://gitee.com/elonelon/dotfiles/raw/master/alias/common.sh >>"$HOME"/.mac.sh &&
        sed -i '' "/.mac.sh/d" "$HOME"/.zshrc &&
        echo "source $HOME/.mac.sh" >>"$HOME/.zshrc" &&
        source "$HOME/.zshrc" &&
        curl -s https://gitee.com/elonelon/dotfiles/raw/master/vimrc >"$HOME"/.vimrc
    echo "success"
}

ssh.login() {
    if [ $# != 1 ]; then
        echo "#### 使用方法: ssh.login root@8.130.179.113"
        return
    fi
    ssh-copy-id -i ~/.ssh/id_rsa.pub $1
}

dc_deploy() {
    compose_dir=$1
    remote=$2

    ssh $remote <<EOF
        docker-compose -f /opt/$(basename $compose_dir)/docker-compose.yml down
        rm -rf /opt/$(basename $compose_dir)
EOF
    scp -r $compose_dir $remote:/opt/$(basename $compose_dir)
    ssh $remote <<EOF
        docker-compose -f /opt/$(basename $compose_dir)/docker-compose.yml up -d
EOF
}

ssh_copy_to_clipboard() {
    ssh-keygen -t rsa -C 'elon@ecs.com' -f ~/.ssh/id_rsa &&
        cat ~/.ssh/id_rsa.pub &&
        pbcopy <~/.ssh/id_rsa.pub &&
        echo 公钥已经复制到剪切板！
}

pl() {
    if [ $# != 1 ]; then
        lsof -n | grep LISTEN
    else
        port=$1
        lsof -n -i:$port | grep LISTEN
    fi
}

kill9() {
    if [ $# != 1 ]; then
        echo "#### 使用方法: kill9 80"
        return
    fi
    port=$1
    kill -9 $(lsof -ti:$port)
}

ssh_ohmyzsh() {
    if [ $# != 1 ]; then
        echo "#### 使用方法: ohmyzsh 134.12.23.87"
        return
    fi
    ip=$1
    curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh -o install_oh-my-zsh.sh
    ssh root@$ip 'apt update && apt install git zsh -y && chsh -s /bin/zsh'
    ssh root@$ip <install_oh-my-zsh.sh
    rm -rf install_oh-my-zsh.sh
}

ssh_init() {
    if [ $# != 1 ]; then
        echo "#### 使用方法: ssh_init root@134.12.23.87"
        return
    fi
    target=$1

    cmd="apt update &&
        apt install -y zsh curl wget git ufw pipenv &&
        ufw allow ssh &&
        ufw --force enable"
    ssh $target $cmd
}

ssh_alias() {
    if [ $# != 1 ]; then
        echo "#### 使用方法: ohmyzsh 134.12.23.87"
        return
    fi
    ip=$1

    mkdir debian_alias_elon
    curl -s https://gitee.com/elonelon/dotfiles/raw/master/alias/debian.sh >debian_alias_elon/debian.sh
    curl -s https://gitee.com/elonelon/dotfiles/raw/master/alias/common.sh >>debian_alias_elon/debian.sh
    curl -s https://gitee.com/elonelon/dotfiles/raw/master/vimrc >debian_alias_elon/debian.vimrc

    cmd='sed -i "/.alias.sh/d" "$HOME"/.zshrc && echo "source $HOME/.alias.sh" >>"$HOME/.zshrc" && source "$HOME/.zshrc"'
    scp debian_alias_elon/debian.sh root@$ip:/root/.alias.sh
    scp debian_alias_elon/debian.vimrc root@$ip:/root/.vimrc
    ssh root@$ip $cmd
    rm -rf debian_alias_elon
}

# ssh_config() {
#     if [ $# != 1 ]; then
#         echo "#### 使用方法: ohmyzsh 134.12.23.87"
#         return
#     fi
#     ip=$1

#     ssh_ohmyzsh $ip
#     ssh_alias $ip
# }

# 安装：
install_aliyun-cli() {
    brew install aliyun-cli
}

# Mac chrome 开启devtools server:
chrome() {
    if [ -f "/Applications/Chromium.app/Contents/MacOS/Chromium" ]; then
        $(/Applications/Chromium.app/Contents/MacOS/Chromium --remote-debugging-port=9222)
        echo "exists"

    elif [ -f "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome" ]; then
        $(/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --remote-debugging-port=9222)

    elif [ -f "/Applications/Microsoft\ Edge.app/Contents/MacOS/Microsoft\ Edge" ]; then
        $(/Applications/Microsoft\ Edge.app/Contents/MacOS/Microsoft\ Edge --remote-debugging-port=9222)
    fi

    # /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --remote-debugging-port=9222 --proxy-server="localhost:8001"
}

go_config_private_gitee_module() {
    git config --global url."git@gitee.com:".insteadOf "https://gitee.com/"

    git config --global url."git@github.com:".insteadOf https://github.com

    go env -w GOPRIVATE="gitee.com/elonelon/*"
}
# 请使用 https://motrix.app/， 已经自己包含ari2c
aria.install() {
    brew install aria2
}
# 请使用 https://motrix.app/， 已经自己包含ari2c
aria.start() {
    open http://aria2c.com/
    mkdir -p "$HOME"/dev/.aria2/
    config_path="$HOME"/dev/.aria2/aria2.conf
    curl -s https://gitee.com/elonelon/dotfiles/raw/master/.aria2/aria2.conf >$config_path
    aria2c --conf-path $config_path
}
# 请使用 https://motrix.app/， 已经自己包含ari2c
aria.stop() {
    killall aria2c
}

clean_android_0() {
    adb shell pm clear com.taobao.taobao              # 淘宝
    adb shell pm clear com.ss.android.ugc.aweme       # 抖音
    adb shell pm clear com.sina.weibo                 # 新浪微博
    adb shell pm clear com.sankuai.meituan            # 美团
    adb shell pm clear com.baidu.netdisk              # 百度网盘
    adb shell pm clear com.cainiao.wireless           # 菜鸟裹裹
    adb shell pm clear com.xunmeng.pinduoduo          # 拼多多
    adb shell pm clear com.ss.android.article.lite    # 今日头条极速版
    adb shell pm clear com.ss.android.article.news    # 今日头条
    adb shell pm clear com.baidu.tieba                # 百度贴吧
    adb shell pm clear com.sankuai.meituan.takeoutnew # 美团外卖
    adb shell pm clear com.taobao.idlefish            # 闲鱼
    adb shell pm clear com.tencent.weread             # 微信读书
    adb shell pm clear com.smile.gifmaker             # 快手
    adb shell pm clear com.ss.android.ugc.live        # 火山小视频
    adb shell pm clear me.ele                         # 饿了么
    adb shell pm clear com.autonavi.minimap           # 高德地图
    adb shell pm clear com.baidu.BaiduMap             # 百度地图
    adb shell pm clear com.sdu.didi.psnger            # 滴滴出行
    adb shell pm clear com.zhihu.android              # 知乎
    adb shell pm clear com.netease.cloudmusic         # 网易云音乐
    adb shell pm clear com.ximalaya.ting.android      # 喜马拉雅
    adb shell pm clear com.tencent.qqmusic            # QQ音乐
    adb shell pm clear com.suning.mobile.ebuy         # 苏宁易购
    adb shell pm clear tv.danmaku.bili                # bilibili
    adb shell pm clear com.tencent.edu                # 腾讯课堂
    adb shell pm clear com.tencent.wework             # 企业微信
    adb shell pm clear com.MobileTicket               # 12306
}

clean_android_1() {
    adb shell pm clear com.hpbr.bosszhipin  # boss直聘
    adb shell pm clear com.tencent.mobileqq # QQ
    adb shell pm clear xxx                  # 钉钉
}

clean_android_2() {
    # 常用的不能清理的：
    adb shell pm clear com.tencent.mm              # 微信
    adb shell pm clear com.eg.android.AlipayGphone # 支付宝
    adb shell pm clear com.alibaba.android.rimet   # 钉钉
    adb shell pm clear com.taobao.qianniu          # 千牛
}

# launchctl相关：
launch_list() {
    if [ $# != 1 ]; then
        launchctl list
    else
        launchctl list | grep "$1"
    fi
}

jr() {
    tail -f /var/log/system.log
}

bin.add() {
    cat <<EOF >>xxx.js
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>com.elon</string>
    <key>ProgramArguments</key>
    <array>
        <string>hello</string>
        <string>world</string>
    </array>
    <key>KeepAlive</key>
    <true/>
</dict>
</plist>
EOF

}

stt() {
    if [ $# != 1 ]; then
        echo "#### 使用方法: stt /path/to/xxx.plist"
    fi
    plist_path=$1
    sudo launchctl load -w $plist_path
    sudo launchctl load $plist_path
}

stp() {
    if [ $# != 1 ]; then
        echo "#### 使用方法: stp /path/to/xxx.plist"
    fi
    plist_path=$1
    sudo launchctl unload -w $plist_path
    sudo launchctl stop $plist_path
}

# nvm:
export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
export NVM_NODEJS_ORG_MIRROR=https://npm.taobao.org/mirrors/node

# vue_init() {
#     if [ $# != 1 ]; then
#         echo "#### 使用方法: vue_init app_name"
#     fi
#     app_name=$1
#     yarn create vite $app_name --template vue
#     vs $app_name
#     cd $app_name

#     yarn add sass sass-loader -D
#     yarn add vue-router@4 vuex@next
#     yarn add axios

#     open "http:localhost:3000"
#     yarn
#     yarn dev
# }

vue_run() {
    yarn
    yarn dev
    open "http:localhost:3000"
}

vue_add_sass() {
    yarn add -D sass sass-loader
}

vue_add_router_vuex() {
    yarn add vue-router@4 vuex@next
}

vue_add_element-plus() {
    yarn add element-plus
}

vue_add_axios() {
    yarn add axios
}

# 自动导入组件的开发插件:
vue_add_unplugin-vue-components() {
    yarn add unplugin-vue-components -D
}

vue_config_format() {
    yarn add eslint eslint-config-prettier eslint-plugin-prettier prettier -D
    cat >.prettierrc.yaml <<EOF
printWidth: 100  # 超过最大值换行
tabWidth: 4 #缩进字节数,默认为2
useTabs: false # 缩进不使用tab，使用空格
semi: false # 句尾添加分号
singleQuote: true # 使用单引号代替双引号
proseWrap: preserve # 默认值。因为使用了一些折行敏感型的渲染器（如GitHub comment）而按照markdown文本样式进行折行
arrowParens: avoid # (x) => {} 箭头函数参数只有一个时是否要有小括号。avoid：省略括号，always：总是有括号
bracketSpacing: true # 在对象，数组括号与文字之间加空格 "{ foo: bar }"
endOfLine: auto # 结尾是 \n \r \n\r auto
eslintIntegration: false # 不让prettier使用eslint的代码格式进行校验
htmlWhitespaceSensitivity: ignore
ignorePath: .prettierignore # 不使用prettier格式化的文件填写在项目的.prettierignore文件中
jsxBracketSameLine: false # 在jsx中把'>' 是否单独放一行
jsxSingleQuote: false # 在jsx中使用单引号代替双引号
requireConfig: false # Require a 'prettierconfig' to format prettier
stylelintIntegration: false # 不让prettier使用stylelint的代码格式进行校验
trailingComma: es5 # 在对象或数组最后一个元素后面是否加逗号（在ES5中加尾逗号）
tslintIntegration: false # 不让prettier使用tslint的代码格式进行校验
EOF

    cat >.eslintrc <<EOF
{
  "extends": [
    "plugin:prettier/recommended"
  ],

  "plugins": ["prettier"],

  "rules": {
    "prettier/prettier": "error"
  }
}
EOF

}

sync_iterm2_settings() {
    vscode_settings_dir="$HOME/code/iterm2_settings"
    vscode_settings_dir_git="$vscode_settings_dir"/.git
    bak_path="$HOME/Library/Application Support/Code/User-bak"

    if [ ! -d "$vscode_settings_dir_git" ]; then
        mv $vscode_settings_dir $bak_path
        git clone git@gitee.com:elonelon/iterm2_settings.git "$vscode_settings_dir"
        cd $vscode_settings_dir
        git config pull.rebase false
        cd -
    else
        cd $vscode_settings_dir
        git add "$vscode_settings_dir"
        git commit -m "."
        git pull
        git push
        cd -
    fi
}

sync_vssettings() {
    vscode_settings_dir="$HOME/Library/Application Support/Code/User"
    vscode_settings_dir_git="$vscode_settings_dir"/.git
    bak_path="$HOME/Library/Application Support/Code/User-bak"

    if [ ! -d "$vscode_settings_dir_git" ]; then
        mv $vscode_settings_dir $bak_path
        git clone git@gitee.com:elonelon/vscode-settings.git "$vscode_settings_dir"
        cd $vscode_settings_dir
        git config pull.rebase false
        cd -
    else
        cd $vscode_settings_dir
        git add "$vscode_settings_dir"
        git commit -m "."
        git pull
        git push
        cd -
    fi
}

sync_alfred_settings() {

    remote_url="git@gitee.com:elonelon/Afred3-Settings.git"
    settings_dir="$HOME/Alfred3-Settings"
    settings_dir_git="$settings_dir"/.git

    if [ ! -d "$settings_dir_git" ]; then
        rm -rf $settings_dir
        git clone "$remote_url" "$settings_dir"
        cd $settings_dir
        git config pull.rebase false
        cd -
    else
        cd $settings_dir
        git add "$settings_dir"
        git commit -m "."
        git pull
        git push
        cd -
    fi
}

syncall() {
    sync_alfred_settings
    sync_vssettings
    sync_iterm2_settings
}

# deploy_frpc() {
#     ip=$1
#     ssh-copy-id -i ~/.ssh/id_rsa.pub admin@$ip
#     frpc_path="/Users/mac-dev/Downloads/frp_0.38.0_linux_amd64/frpc"
#     scp $frpc_path admin@$ip:/opt/frpc
#     ssh admin@$ip "chmod +x /opt/frpc"
#     ssh admin@$ip "frpc http --proxy_name zhangliang --custom_domain zhangliang --server_addr zhangliang.banli.site --local_port 8081"
# }

wid() {
    name=$1
    curl -H "Content-Type:application/json" -X POST --data '{"id": "'"$name"'"}' http://banli.site/nas/registerId
}

wfrp() {
    /Users/mac-dev/code/go_proj/frp_deploy/frp/Mac/frpc http --proxy_name $1 --custom_domain $1.myunas.cn --server_addr $1.myunas.cn:7000 --local_port 80
}

# 查看shell函数定义
alias tsf="typeset -f"

sd_deploy() {
    dir=$1
    ip=$2
    dir_name="$(basename $dir)"
    scp -r $dir root@$ip:/opt/
    ssh root@$ip <<EOF
        find /opt/$dir_name -type f ! -name "*.*" -exec chmod +x {} \;
        find /opt/$dir_name -type f -name "*.service" -exec ln -s {} /etc/systemd/system/ \;
        systemctl daemon-reload
        find /opt/$dir_name -type f -name "*.service" -execdir bash -c 'systemctl start \${2#./}' - "\$1" '{}' \;
        find /opt/$dir_name -type f -name "*.service" -execdir bash -c 'systemctl enable \${2#./}' - "\$1" '{}' \;
        find /opt/$dir_name -type f -name "*.service" -execdir bash -c 'systemctl status \${2#./}' - "\$1" '{}' \;
EOF
}

sd_undeploy() {
    dir=$1
    ip=$2
    dir_name="$(basename $dir)"
    ssh root@$ip <<EOF
        find /opt/$dir_name -type f -name "*.service" -execdir bash -c 'systemctl stop \${2#./}' - "\$1" '{}' \;
        find /opt/$dir_name -type f -name "*.service" -execdir bash -c 'systemctl disable \${2#./}' - "\$1" '{}' \;
        find /opt/$dir_name -type f -name "*.service" -execdir bash -c 'rm -rf /etc/systemd/system/\${2#./}' - "\$1" '{}' \;
        systemctl daemon-reload
        rm -rf /opt/$dir_name
EOF
}

load_plist() {
    file=$1
    ln -s $file $HOME/Library/LaunchAgents/$(basename $file)
    launchctl load -w $file
}

unload_plist() {
    file=$1
    launchctl unload -w $HOME/Library/LaunchAgents/$(basename $file)
    rm -rf $HOME/Library/LaunchAgents/$(basename $file)
}

ttt1() {
    id=$1
    curl -H "Content-Type:application/json" -X POST --data '{"id": "'"$id"'", "token":"xxxxxxxxxxx", "ts": "1234567890", "http_name": "'"$id"'", "custom_domain": "'$id'.myunas.cn", "product_license":"ttt1"}' https://supernode.u-nas.cn/v1/nas/registerId
}

ttt2() {
    id=$1
    curl -H "Content-Type:application/json" -X POST --data '{"id": "'"$id"'", "token":"xxxxxxxxxxx", "ts": "1234567890", "http_name": "'"$id"'", "custom_domain": "'$id'.myunas.cn", "product_license":"ttt2"}' https://supernode.u-nas.cn/v1/nas/registerId
}

set.proxy.no() {
    export no_proxy="*"
}

set.proxy() {
    export https_proxy=http://127.0.0.1:7890
    export http_proxy=http://127.0.0.1:7890
    export all_proxy=socks5://127.0.0.1:7890
    export no_proxy="localhost, 127.0.0.1"
}

function proxy.lantern() {
    export https_proxy=http://127.0.0.1:56559
    export http_proxy=http://127.0.0.1:56559
    export all_proxy=socks5://127.0.0.1:56559
    export no_proxy="localhost, 127.0.0.1"
}

ssh.config() {
    remote=$1
    ssh $remote <<EOF
        sed -i "/ClientAliveInterval/d" /etc/ssh/sshd_config
        sed -i "/ClientAliveCountMax/d" /etc/ssh/sshd_config

        echo "ClientAliveInterval 60" >> /etc/ssh/sshd_config
        echo "ClientAliveCountMax 3" >> /etc/ssh/sshd_config

        systemctl reload sshd.service
EOF
}

# ssh 端口转发-反向代理:
ssh.port.reverse() {
    if [ $# != 2 ]; then
        echo "#### 使用方法: ssh.port.reverse root@banli.site 8888"
        return
    fi

    remote=$1
    port=$2

    ssh $remote <<EOF
        kill -9 $(lsof -t -i:$port) 1>/dev/null  2>&1
        sed -i "/GatewayPorts yes/d" /etc/ssh/sshd_config
        echo "GatewayPorts yes" >> /etc/ssh/sshd_config
        systemctl reload sshd.service
EOF
    ssh -CqTfnN -R 0.0.0.0:$port:127.0.0.1:$port $remote
}

# ssh 端口转发-正向代理:
ssh.port() {
    if [ $# != 2 ]; then
        echo "#### 使用方法: ssh.port root@banli.site 8888"
        return
    fi
    remote=$1
    port=$2
    ssh -fNL "$port":localhost:"$port" "$remote"
}

ssh_init() {
    remote=$1
    ssh $remote <<EOF


        ufw allow http && \
        ufw allow https && \
        ufw allow 7000 && \
        ufw allow 8001 && \

        history -c


        # apt update && \
        # apt install -y sudo zsh curl wget git ufw && \
        # ufw allow ssh && \
        # ufw --force enable && \
        # chsh -s /bin/zsh && \
        # sh -c "$(wget https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh -O -)"
EOF
}

# 清理dns缓存:
dnsclean() {
    sudo killall -HUP mDNSResponder
    dscacheutil -flushcache sudo discoveryutil udnsflushcaches
}

# 查询dns:
dns() {
    domain=$1
    nslookup $domain
}

n.init() {
    npm init -yp
}

y.init() {
    yarn init -yp
}

tsc.init() {
    tsc --init
}

#############################################
init.vue3() {
    if [ $# != 1 ]; then
        echo "#### 使用方法: init.vue3 project_name_xxx"
        return
    fi
    proj_name=$1
    yarn create vite $proj_name --template vue
    cd $proj_name
    yarn
    code .
}

init.vue3.ts() {
    if [ $# != 1 ]; then
        echo "#### 使用方法: init.vue3.ts project_name_xxx"
        return
    fi

    proj_name=$1
    yarn create vite $proj_name --template vue-ts
    cd $proj_name
    yarn
    code .
}

test_chome_start() {
    /Applications/Chromium.app/Contents/MacOS/Chromium --disable-web-security --disable-web-security --remote-debugging-port=9000 --proxy-server=localhost:8000
}

brew.link() {
    brew link --overwrite --force
}

# php相关:
if [[ $(uname -m) == 'arm64' ]]; then
    export PATH="/opt/homebrew/opt/php@8.0/bin:$PATH"
    export PATH="/opt/homebrew/opt/php@8.0/sbin:$PATH"
fi

alias nmap="nmap -Pn"

test111() {
    mkdir -p /test/r && chmod 754 /test/r
    mkdir -p /test/w && chmod 752 /test/w
    mkdir -p /test/rw && chmod 755 /test/rw
    mkdir -p /test/xw && chmod 753 /test/xw
    mkdir -p /test/xw && chmod 753 /test/xw
    mkdir -p /test/xrw && chmod 757 /test/xrw

    getent passwd | grep -oP '^[^:]+(?=:x:\d{4}:)'
}

# 创建新用户:

l.user.new() {
    username=$1
    password=$2

    adduser --gecos "" --disabled-password $username
    chpasswd <<<"$username:$password"
}

# ssh-修改登录端口:
sshd.change.port() {
    if [ ! -n "$1" ]; then
        echo "$1 使用方式：sshd.change.port 23"
        return
    fi

    port=$1
    sed -ri "s/^#?Port\s+.*/Port $port/" /etc/ssh/sshd_config
    systemctl restart sshd

    apt update
    apt install ufw -y
    ufw allow $port
    ufw enable
}

# ssh-禁止密码登录:
sshd.disable.pwd() {
    sed -ri 's/^#?PasswordAuthentication\s+.*/PasswordAuthentication no/' /etc/ssh/sshd_config
    # 禁止密码登录的同时，一定要再三确保开启公钥登录:
    sed -ri 's/^#?PubkeyAuthentication\s+.*/PubkeyAuthentication yes/' /etc/ssh/sshd_config
    systemctl restart sshd
}

# ssh-开启密码登录:
sshd.enable.pwd() {
    sed -ri 's/^#?PasswordAuthentication\s+.*/PasswordAuthentication yes/' /etc/ssh/sshd_config
    systemctl restart sshd
}

# 禁止ssh登录root用户
sshd.disable.root() {
    # 先查询当前有没有其他用户:
    user_count=$(getent passwd | grep -oP '^[^:]+(?=:x:\d{4}:)' | wc -l)
    if [ $user_count -eq 0 ]; then
        echo "操作失败：除root外，没有其他可登录用户! 请使用adduser xxx去新建用户"
        return
    fi
    echo "其他可ssh登录用户:"
    getent passwd | grep -oP '^[^:]+(?=:x:\d{4}:)'

    # 禁止root登录:
    sed -ri 's/^#?PermitRootLogin\s+.*/PermitRootLogin no/' /etc/ssh/sshd_config
    systemctl restart sshd
}

# 开启ssh登录root用户
sshd.enable.root() {
    sed -ri 's/^#?PermitRootLogin\s+.*/PermitRootLogin yes/' /etc/ssh/sshd_config
    systemctl restart sshd
}

l.user.del() {
    username=$1
    file=/etc/sudoers.d/$username
    if [ -f $file ]; then
        rm -rf $file
    fi

    userdel -r $username
}

l.user.new.superadmin() {
    username=$1
    password=$2

    # 需要先确保安装了sudo,要不然不存在/etc/sudoers.d目录:
    if ! which sudo >/dev/null 2>&1; then
        apt update && apt install sudo -y
    fi

    # 创建新用户，赋予sudo权限:
    adduser --gecos "" --disabled-password $username
    chpasswd <<<"$username:$password"
    # echo "$username  ALL=(ALL:ALL) ALL" >>/etc/sudoers.d/$username           # sudo时需要输入普通用户密码
    echo "$username  ALL=(ALL:ALL) NOPASSWD: ALL" >>/etc/sudoers.d/$username # sudo时不需要输入普通用户密码

    echo "$username ALL=NOPASSWD: /bin/su –" >>/etc/sudoers.d/$username # su -时不需要输入root密码

    # 关闭root用户ssh登录能力:
    sed -i "/PermitRootLogin yes/d" /etc/ssh/sshd_config 1>/dev/null 2>&1
    echo "PermitRootLogin no" >>/etc/ssh/sshd_config
    systemctl restart sshd

    # 更改用户的登录shell:
    if ! which zsh >/dev/null 2>&1; then
        apt update && apt install zsh -y
    fi
    chsh -s $(which zsh) $username
}

# 创建用户：1. 创建home目录  2. 创建默认同名group 3.允许登录
function add_user() {
    username=$1
    password=$2
    adduser --gecos "" --disabled-password $username
    chpasswd <<<"$username:$password"
    # 使用示例：add_user elon elonpwd
}

# 给用户添加sudo权限:
function add_sudo() {
    username=$1
    echo "$username  ALL=(ALL:ALL) NOPASSWD: ALL" >>/etc/sudoers.d/$username # sudo时不需要输入普通用户密码
}

# 删除sudo权限:
function delete_sudo() {
    username=$1
    rm -rf /etc/sudoers.d/$username
}

function enable_root_login() {
    sed -i "/PermitRootLogin /d" /etc/ssh/sshd_config 1>/dev/null 2>&1
    echo "PermitRootLogin yes" >>/etc/ssh/sshd_config
    systemctl restart sshd
}

function disable_root_login() {
    sed -i "/PermitRootLogin /d" /etc/ssh/sshd_config 1>/dev/null 2>&1
    echo "PermitRootLogin no" >>/etc/ssh/sshd_config
    systemctl restart sshd
}

add.user.sudo() {
    username=$1
    echo "$username  ALL=(ALL:ALL) ALL" >>/etc/sudoers.d/$username
}

# 用户列表:
l.user.list() {
    cat /etc/passwd | cut -d: -f1
}

alias tf="typeset -f"

install.tailwindcss() {
    pnpm add -D tailwindcss@latest postcss@latest autoprefixer@latest
    npx tailwindcss init -p # 生成 tailwind.config.js 和 postcss.config.js 文件

    cat <<EOF >>tailwind.css
/* 在main.js中引入改文件: import './tailwind.css' */
@tailwind base;
@tailwind components;
@tailwind utilities;
EOF

}
